package com.trailwalker.traceability.widget.control.textfield;

import com.trailwalker.JavaUtils.string.StringUtils;
import com.trailwalker.javafx.utils.resource.ResourceUtils;
import com.trailwalker.javafx.utils.style.AdaptUtils;
import com.trailwalker.javafx.utils.style.StyleUtils;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;


/**
 * @author Trailwalker
 * @create 2021-11-12 15:44
 */
public class ScanTextFiled extends HBox {

    private AdaptUtils mAdaptUtils;
    private TextField mTextField;
    private Label mLabel;
    private boolean mIsShowValue = true;
    private String mValue;
    private OnScanFinishedListener mOnScanFinishedListener;

    public ScanTextFiled() {
        onInit();
    }

    private void onInit() {
        initLoad();
        initView();
        initData();
        initListener();
        onReady();
    }

    public String getText() {
        return mValue == null ? "" : mValue;
    }

    public void setText(String text) {
        mValue = text;
        mLabel.setText(mValue);
    }

    public void clear() {
        setText("");
    }

    public void requestFocus() {
        if (mTextField != null) {
            mTextField.requestFocus();
        }
    }

    public void setIsShowValue(boolean isShowValue) {
        mIsShowValue = isShowValue;
    }

    public void setOnScanFinishedListener(OnScanFinishedListener onScanFinishedListener) {
        mOnScanFinishedListener = onScanFinishedListener;
    }

    private void onReady() {

    }

    private void initListener() {
        mTextField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if (StringUtils.isValid(newValue)) {
                    mLabel.setVisible(false);
                }
            }
        });

        mTextField.setOnKeyReleased(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if (event.getCode().equals(KeyCode.BACK_SPACE) || event.getCode().equals(KeyCode.DELETE)) {
                    if (StringUtils.isEmpty(mTextField.getText())) {
                        clear();
                    }
                }
                if (event.getCode() == KeyCode.ENTER) {
                    String text = mTextField.getText();
                    if (StringUtils.isValid(text)) {
                        mValue = text;
                        mTextField.clear();
                        mLabel.setText(mValue);
                        if (mIsShowValue) {
                            mLabel.setVisible(true);
                        }
                    }
                    if (mOnScanFinishedListener != null) {
                        mOnScanFinishedListener.onFinish(mValue);
                    }
                }
            }
        });
    }

    private void initData() {

    }

    private void initView() {
        this.setAlignment(Pos.CENTER);
        StyleUtils.setBorderColor(this, "#909090");

        ImageView imageView = new ImageView();
        this.getChildren().add(imageView);
        imageView.setFitWidth(mAdaptUtils.getScale(30));
        imageView.setFitHeight(mAdaptUtils.getScale(30));
        imageView.setImage(new Image(ResourceUtils.getImage(this, "scan.png")));
        HBox.setMargin(imageView, mAdaptUtils.getMarginLeftAndRight(10, HBox.getMargin(imageView)));

        AnchorPane apTfContainer = new AnchorPane();
        this.getChildren().add(apTfContainer);
        HBox.setHgrow(apTfContainer, Priority.ALWAYS);
        mAdaptUtils.setScaleHeight(apTfContainer, 50);

        mLabel = new Label();
        apTfContainer.getChildren().add(mLabel);
        mAdaptUtils.setScaleTextSize(mLabel, 30);
        mAdaptUtils.setMatchParentAnchorPane(mLabel);
        StyleUtils.setTextColor(mLabel, "#484848");
        mLabel.setVisible(false);

        mTextField = new TextField();
        apTfContainer.getChildren().add(mTextField);
        mAdaptUtils.setScaleTextSize(mTextField, 30);
        mAdaptUtils.setMatchParentAnchorPane(mTextField);
        StyleUtils.setBorderColor(mTextField, "#ffffff00");
    }

    private void initLoad() {
        mAdaptUtils = AdaptUtils.getInstance();
    }

    public interface OnScanFinishedListener {
        void onFinish(String value);
    }
}
